define(["require", 'config/appregister', 'business/login/service/http', "jquery", "domReady"], function(require, app, httpServices) {
	app.controller('emailmodifyCtrl', function($scope, $state) {
		var input1 = $(".newemail").val();
		var input2 = $(".newemailto").val();
		var box = $(".emailmod-bg");

		function getQueryString(name) {
			// 如果链接没有参数，或者链接中不存在我们要获取的参数，直接返回空 
			if(location.href.indexOf("?") == -1 || location.href.indexOf(name + '=') == -1) {
				return '';
			}

			// 获取链接中参数部分 
			var queryString = location.href.substring(location.href.indexOf("?") + 1);

			// 分离参数对 ?key=value&key2=value2 
			var parameters = queryString.split("&");

			var pos, paraName, paraValue;
			for(var i = 0; i < parameters.length; i++) {
				// 获取等号位置 
				pos = parameters[i].indexOf('=');
				if(pos == -1) {
					continue;
				}
				// 获取name 和 value 
				paraName = parameters[i].substring(0, pos);
				paraValue = parameters[i].substring(pos + 1);

				// 如果查询的name等于当前name，就返回当前值，同时，将链接中的+号还原成空格 
				if(paraName == name) {
					return unescape(paraValue.replace(/\+/g, " "));
				}
			}
			return '';
		};

		var email = getQueryString("email");
		var verifyCode = getQueryString("verifyCode");
		var verifyToken = getQueryString("verifyToken");
		check();
		function check() {
			var req = {
				type:'POST',
				uri:'forgetPasswdByEmailWithCheckPhoneCode',
				data:{
					email:email,
					verifyCode:verifyCode,
					verifyToken:verifyToken					
				}
			};
			httpServices.checkemlcodeHttpService(req).success(function(res){
				if(res.code==0) {
					$(box).show();
				}else {
					alert(res.msg);
				}
			}).error(function(res){
				alert("流程三忘记密码邮箱验证码校验请求失败")
			});
		};

		$scope.postnewpswd = function() {
			var input1 = $(".newemail").val();
			var input2 = $(".newemailto").val();

			if(input1 == input2 && input1 !== "" && input2 !== "") {
				var req = {
					type:'POST',
					uri:'forgetPasswdByEmailWithCommitNewPasswd',
					data:{
						newPasswd: input1,
						verifyToken: verifyToken
					}
				};
				$.ajax({
					type:"post",
					url:"http://"+allUrl+":8080/elink_user/api/user/forgetPasswdByEmailWithCommitNewPasswd",
					data:req.data,
					success:function(data) {
						$scope.$apply(function(){
							if(data.code==0) {
								$state.go("eng.loginedReseted");
							}else {
								alert(data.msg);
							}
						})
					},
					error:function() {
						alert("忘记密码通过邮箱验证修改新密码请求失败")
					}
				});
			} else {
				$(".tip").show().html("输入错误").css("color", "red");
			}

		}

	});
});